package com.yanqu.road.server.cmd.activity.jiuzhou.cross;

import com.yanqu.road.entity.activity.jiuzhou.JiuZhouUserBaseData;
import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouConfig;
import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouRoundConfig;
import com.yanqu.road.entity.enums.activity.jiuzhou.eJiuZhouRoundType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.jiuzhou.CrossJiuZhouProto;
import com.yanqu.road.pb.activity.jiuzhou.JiuZhouProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.jiuzhou.JiuZhouModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.jiuzhou.JiuZhouMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_JIU_ZHOU_GET_GAME_DATA_FROM_CROSS, desc = "拉取游戏数据")
public class JiuZhouGetGameDataFromCrossCmd extends AbstractCommand {

    private CrossJiuZhouProto.CrossJiuZhouGameDataRespMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = CrossJiuZhouProto.CrossJiuZhouGameDataRespMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        JiuZhouConfig config = JiuZhouMgr.getConfig();
        if (config == null) {
            return;
        }
        JiuZhouUserBaseData userData = player.getModule(JiuZhouModule.class).getUserData();
        if (userData == null) {
            return;
        }
        JiuZhouProto.JiuZhouGameDataRespMsg.Builder respMsg = JiuZhouProto.JiuZhouGameDataRespMsg.newBuilder();
        if (reqMsg.hasGameData()) {
            int round = reqMsg.getGameData().getRound();
            JiuZhouRoundConfig roundConfig = config.getRoundConfigMap().get(round);
            // 判断是否有资格,有资格的不给
            if (roundConfig.getType() == eJiuZhouRoundType.knockout.getValue() && !reqMsg.getAble()) {
                // 淘汰赛每轮可领取声援券
                long zeroTimeStamp = DateHelper.getTodayZeroTimeStamp();
                if (zeroTimeStamp > userData.getSupportRewardTime()) {
                    userData.setSupportRewardTime(zeroTimeStamp);
                    // 获取奖励
                    Property supportReward = new Property(config.getMoneyItemId3(), config.getSupportNum());
                    player.getModule(CurrencyModule.class).addCurrency(supportReward, eLogMoneyType.JiuZhou, eLogMoneyType.JiuZhouEveryDaySupportReward);

                    respMsg.setSupportReward(PropertyHelper.parsePropertyToString(supportReward));
                }
            }
            respMsg.setGameData(reqMsg.getGameData());
        }
        respMsg.setRet(0);


        player.sendPacket(Protocol.U_JIU_ZHOU_GAME_DATA_REQ, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
